package com.itheima.service;

import com.itheima.domain.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class StudentServiceTest2 {
  //查询全部
@Test
    public void findAll(){
    //1.获取SqlSession对象
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //2.获取mapper的代理对象
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //3.调用代理对象方法
    List<Student> list = mapper.findAll();
    for (Student student : list) {
        System.out.println(student);
    }
    sqlSession.close();
}
//根据进行查询
@Test
    public void  findById( ){
    //获取sqlsession对象
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //获取mapper的代理对象
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //调用代理对象方法
    Student byId = mapper.findById(1);
    System.out.println(byId);
    //关闭资源
    sqlSession.close();

}
@Test
    public void save(){
    //获取sqlsession对象
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //获取代理对象mapper
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //调用代理对象方法
    Student stu = new Student(8,"貂蝉2",23);
    Integer save = mapper.save(stu);
    System.out.println(save);
    sqlSession.close();

}
@Test
    public void update(){
    //获取sqlsession
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //获取代理对象
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //调用代理对象方法
    Student stu = new Student(7,"宋祖儿",23);
    Integer update = mapper.update(stu);
    System.out.println(update);

    sqlSession.close();
}
@Test
    public void delete(){
    //获取sqlsession
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //获取代理对象
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //调用代理对象方法
    Integer delete = mapper.delete(8);
    System.out.println(delete);
    sqlSession.close();
}
@Test
    public void findByName(){
    //获取sqlsession
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //获取代理对象
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //调用代理对象方法
    List<Student> students = mapper.findByName("楠");
    System.out.println(students);
    sqlSession.close();
}
@Test
    public void findByNameOrAge(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    List<Student> nameOrAge = mapper.findByNameOrAge("儿", 23);
    System.out.println(nameOrAge);
    sqlSession.close();
}
@Test
    public void findStus(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    Student student = new Student(3,"赵",23);
    List<Student> stus = mapper.findStus(student);
    System.out.println(stus);
    sqlSession.close();
}
    @Test
    public void findIds(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Integer> list = new ArrayList<>();
        for(int i=1; i<=10 ;i++){
            list.add(i);
        }

        List<Student> stus = mapper.findIds(list);
        for (Student student : stus) {
            System.out.println(student);
        }

        sqlSession.close();
    }

}
